﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls.WebParts;
using UFSoft.UBF.UI.ControlModel;

namespace UFIDA.U9.CUST.AITBarCodeUI.utils
{
    internal class UIUtils
    {
        /// <summary>
        /// 弹出列表
        /// </summary>
        /// <param name="part">当前part</param>
        /// <param name="btnReload">回调按钮</param>
        /// <param name="partid">弹出partid,代码文件中可查看</param>
        /// <param name="title">弹出标题</param>
        public static void ShowListDialog(AITBarCodeFormWebPart part, IUFControl btnReload,string partid,string title)
        {
            part.ShowAtlasModalDialog(btnReload, partid, title, "992", "504", null, null, true, false, false);
        }
        /// <summary>
		/// 刷新Grid界面
		/// </summary>
        /// <param name="part">界面PART</param>
		/// <param name="ids">需要加载的id集合,如果为空,加载当前界面id集合</param>
        public static void RefreshGridUI(AITBarCodeFormWebPart part,List<long> ids)
        {
            //清除报错红点
            part.Model.ClearErrorMessage();

            if (ids == null || ids.Count == 0)
            {
                ids = getCurrentIDS(part.Model);
            }

            if (ids != null && ids.Count > 0) {
                
                // 清除view,强制加载
                part.Model.AITBarCode.Locked = false;
                part.Model.AITBarCode.Clear();
                part.Model.AITBarCode.Cache.Locked = false;
                part.Model.AITBarCode.ClearCache();

                LoadModelData(part, ids);
            }
        }
        /// <summary>
        /// 获取当前Model的ID集合
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<long> getCurrentIDS(AITBarCodeUIModel model)
        {
            List<long> ids = new List<long>();
            //传入空,默认加载当前界面id集合
            foreach (var rec in model.AITBarCode.Records)
            {
                ids.Add((long)rec["ID"]);
            }

            return ids;
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="part"></param>
        /// <param name="ids"></param>
        public static void LoadModelData(AITBarCodeFormWebPart part, List<long> ids)
        {
            // 将 List<long> 转换为逗号分隔的字符串
            string idsstr = string.Join(",", ids);
            // 修改加载条件
            part.Model.AITBarCode.CurrentFilter.OPath = $"ID in ({idsstr})";
            // 下述两种方法都可以重新加载数据,调用一个就行,不用同时调用两个
            part.Action.CommonAction.Load(part.Model);
            part.Model.AITBarCode.Records.Sort(new string[] { "ID" });
            //part.Action.NavigateAction.Refresh(null);
        }
    }
}
